The data URI scheme is a URI scheme (Uniform Resource Identifier scheme) that provides a way to include data in-line in web pages as if they were external resources. It tends to be simpler than other inclusion methods, such as MIME with cid or mid URIs. Data URIs are sometimes called Uniform Resource Locators, although they do not actually locate anything remote. The data URI scheme is defined in RFC 2397 of the Internet Engineering Task Force (IETF).
The IETF published the data URI specification in 1998[1] as Proposed Standard on the IETF Standards Track, and hasn't progressed it since. The HTML 4.01 specification refers to the data URI scheme,[2] and data URIs have now been implemented in most browsers.
Contents |
Data URIs are currently supported by the following web browsers:
object
(images only)img
input type=image
link
(data URI must be base64 encoded)background-image
, background
, list-style-type
, list-style
and similar.If a website designer is using data URIs they can support other browsers by serving different content based on the browsers User-Agent string.[5]
multipart/alternative
or message/rfc822
) to provide greater complexity such as metadata, data compression, or content negotiation.data:[<MIME-type>][;charset=<encoding>][;base64],<data>
The encoding is indicated by ;base64
. If it's present the data is encoded as base64. Without it the data (as a sequence of octets) is represented using ASCII encoding for octets inside the range of safe URL characters and using the standard %xx hex encoding of URLs for octets outside that range. If <MIME-type>
is omitted, it defaults to text/plain;charset=US-ASCII
. (As a shorthand, the type can be omitted but the charset parameter supplied.)
Some browsers (Chrome, Opera, Safari, Firefox) accept a non-standard ordering if both ;base64
and ;charset
are supplied, while Internet Explorer requires that the charset's specification must precede the base64 token.
An HTML fragment embedding a picture of small red dot:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
As demonstrated above, data URIs encoded with base64 may contain whitespace for readability.
A CSS rule that includes a background image:
ul.checklist li.complete { margin-left: 20px; background: white url('data:image/png;base64,iVBORw0KGgoAA AANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0l EQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6 P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC') no-repeat scroll left top; }
In Mozilla Firefox 5 (released June, 2011), encoded data must not contain newlines.
A JavaScript statement that opens an embedded subwindow, as for a footnote link:
window.open('data:text/html;charset=utf-8,%3C%21DOCTYPE%20' + 'html%3E%0D%0A%3Chtml%20lang%3D%22en%22%3E%0D%0A%3Chead%' + '3E%3Ctitle%3EEmbedded%20Window%3C%2Ftitle%3E%3C%2Fhead%' + '3E%0D%0A%3Cbody%3E%3Ch1%3E42%3C%2Fh1%3E%3C%2Fbody%3E%0A' + '%3C%2Fhtml%3E%0A%0D%0A','_blank','height=300,width=400');
This example does not work with Internet Explorer 8 due to its security restrictions that prevent navigable file types from being used.[4]
Because base64-encoded data URIs are not human readable, a website author might prefer the encoded data be included in the page via a scripting language such as PHP. This has the advantage that if the included file changes, no modifications need to be made to the HTML file, and also of keeping a separation between binary data and text based formats. Disadvantages include greater server CPU use unless a server-side cache is used.
<?php function data_uri($file, $mime) { $contents = file_get_contents($file); $base64 = base64_encode($contents); return "data:$mime;base64,$base64"; } ?> <img src="<?php echo data_uri('elephant.png', 'image/png'); ?>" alt="An elephant">
Similarly, if CSS is processed by PHP, the below function may also be used:
<?php header('Content-type: text/css'); ?> div.menu { background-image:url('<?php echo data_uri('elephant.png', 'image/png'); ?>'); }
In either case, client or server side features (such as dynamic content generation), client detection and awareness (to support selection of alternative content, such as a different language), or discrimination (content filtering based on some client deficiency) systems (like conditional comments) may be used to provide a standard http: URL for Internet Explorer and other older browsers.
data_uri = open("sample.png", "rb").read().encode("base64").replace("\n", "") img_tag = '<img alt="sample" src="data:image/png;base64,{0}">'.format(data_uri) print img_tag
import base64 data_uri = str(base64.encodestring(open("sample.png", "rb").read()) , "utf8").replace("\n", "") img_tag = '<img alt="sample" src="data:image/png;base64,{0}">'.format(data_uri) print(img_tag)
|
|